package view.ui
{
	import control.manager.BitmapManager;
	import control.manager.LayerManager;
	import control.manager.RoleManager;
	import control.manager.TaskManager;
	
	import data.GameConst;
	import data.PlayerData;
	import data.TaskVO;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.geom.Rectangle;
	
	import ghostcat.util.easing.TweenUtil;
	
	import view.Immortal;
	import view.layer.BaseLayer;
	import view.layer.GameLayer;
	import view.layer.RoleLayer;
	import view.role.MapNPC;
	import view.role.MapPlayer;
	
	public class UIPopupLayer extends BaseLayer
	{
		
		private static var _I :UIPopupLayer = null;
		public  var curShowUI :*;
		/**
		 * 正在显示谈话框
		 */		
		public var isShow :Boolean = false;
		
		public var mcLoading :MovieClip;
		public function UIPopupLayer(prv :PrivateClass)
		{
			super();
		}
		
		public function showLoading():void
		{
			if(isShow)
			{
				return;
			}
			if(mcLoading)
			{
				if(mcLoading.parent)
				{
					mcLoading.parent.removeChild(mcLoading);
					mcLoading = null;
				}
			}
			this.mcLoading = BitmapManager.instance.getMCByName(GameConst.MC_LOADING);
			if(this.mcLoading)
			{
				this.mcLoading.x = stage.stageWidth/2;
				this.mcLoading.y = stage.stageHeight/2;
				this.addChild(this.mcLoading);
			}
			Immortal.instance.gameStage.enabled = false;
		}
		
		public function hideLoading():void
		{
			if(mcLoading)
			{
				if(mcLoading.parent)
				{
					mcLoading.parent.removeChild(mcLoading);
					mcLoading = null;
				}
			}
			Immortal.instance.gameStage.enabled = true;
		}
		/**
		 * 隐藏弹出框
		 */		
		public function hide():void
		{
			if(curShowUI)
			{
				curShowUI.destroy();
				if(curShowUI.parent)
				{
					this.removeChild(this.curShowUI);
				}
				isShow = false;
			}
		}
		/**
		 * 显示弹出框 
		 * @param display  必须继承UILoadingBase 自动加载UI基类
		 * 
		 */		
		public function show(display :DisplayObject):void
		{
			
			if(isShow) return;
			
			hide();
			this.addChild(display);
			this.curShowUI = display;
			this.visible = true;
			isShow = true;
		}
		/**
		 * 第一次进入游戏的任务弹窗 
		 * 
		 */		
		public function firstTaskShow():void
		{
			var value :Boolean = TaskManager.instance.isFirstGame2Task();
			if(value)
			{
				var npc :MapNPC = RoleManager.instance.findFirstTaskNPC();
				
				if(!npc || !npc.npcData.taskList || npc.npcData.taskList.length == 0)
				{
					trace("没有找到第一任务NPC,或NPC身上的任务列表为空");
					return;
				}
				//第一次任务弹窗
				var ui :UITaskTalk = new UITaskTalk(npc,true);
				this.show(ui);
				isShow = true;
				GameConst.isFirstGame = false;
			}
		}

		public static function get I():UIPopupLayer
		{
			if(_I == null)
			{
				_I = new UIPopupLayer(new PrivateClass());
			}
			return _I;
		}
		
		public function resize():void
		{
			if(stage)
			{
				this.curShowUI.x = (stage.stageWidth - this.width)/2;
				this.curShowUI.y = (stage.stageHeight - this.height)/2;
			}
		}

	}
}

class PrivateClass{}